package com.fishery.processing.controller;


import com.fishery.entity.Result;
import com.fishery.processing.client.OrderClient;
import com.fishery.processing.entity.Product;
import com.fishery.processing.entity.client.OrderVo;
import com.fishery.processing.service.ProductBatchService;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author run
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/processing/batch")
@CrossOrigin
@Api(tags = "产品批次相关接口")
public class ProductBatchController {

    @Autowired
    private ProductBatchService batchService;

    @Autowired
    private OrderClient orderClient;

    @GetMapping("/{id}/{page}/{size}")
    @ApiOperation("根据加工厂id分页获取产品批次列表")
    public Result getBatchOfFactory(@PathVariable(value = "id") String factoryId
                                        , @PathVariable(value = "page") Integer page
                                        , @PathVariable(value = "size") Integer size){
        return Result.success().data(batchService.getBatchOfFactory(factoryId, page, size));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据产品批次id获取批次信息（订单模块调用）")
    public Result getBatchById(@PathVariable(value = "id") String batchId){
        return Result.success().data(batchService.getBatchById(batchId));
    }

    @PutMapping("/{id}/{count}")
    @ApiOperation("根据产品批次id调整库存（订单模块调用）")
    public Result setInventoryOfBatch(@PathVariable(value = "id") String batchId
                                        , @PathVariable(value = "count") Double weight){
        return batchService.setInventoryOfBatch(batchId, weight)
                ? Result.success("调整成功") : Result.error("调整失败");
    }

    @GetMapping("/root/{id}")
    @ApiOperation("根据产品批次id获取相关信息（溯源模块调用）")
    public Result getMessageOfBatch(@PathVariable(value = "id") String batchId){
        return Result.success().data(batchService.getMessageOfBatch(batchId));
    }

    @GetMapping("/before/{id}")
    @ApiOperation("根据产品批次id获取原料的id和原料来源方类型")
    public Result getSourceMessage(@PathVariable(value = "id") String batchId){
        return Result.success().data(batchService.getBeforeMessageOfBatch(batchId));
    }

    @PutMapping("/order")
    @ApiOperation("创建订单")
    public Result createOrder(@RequestBody @ApiParam(required = true,value = "orderVo") OrderVo orderVo){
        return orderClient.createOrder(orderVo);
    }



}

